<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: google_analytics.php 11 2013-09-21 11:43:11Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 11 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:43:11 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/functions/widgets/gapi.class.php');

$msg = '';
$data_ok = false;

if(isset($_GET['ga']) && $_GET['ga'] == 'delete') {
	$db->db_query("UPDATE ".TABLE_ADMIN_START_PAGE." SET parameter = '' WHERE php_name = 'google_analytics' ");
	$msg = 'Ihre Analytics-Daten wurden gelöscht!';
}

$data = $db->db_query("SELECT parameter FROM ".TABLE_ADMIN_START_PAGE." WHERE php_name = 'google_analytics'");
if($data->_numOfRows && $data->fields['parameter'] !='') {
	$ga_data = explode('|', $data->fields['parameter']);
	foreach($ga_data AS $gd) {
		$key = explode(':', $gd);
		$vars[$key[0]] = $key[1];
	}

	if($vars['email']!='' && $vars['password'] !='' && $vars['id'] !='')
		$data_ok = true;

} elseif((isset($_POST['submit_data']) && $_POST['submit_data'] == 'yes') && (!empty($_POST['ga_email_address'])) && (!empty($_POST['ga_password']))) {


	if(isset($_POST['save_permanently']) && $_POST['save_permanently'] == 'yes') {
		$params = 'email:'.$_POST['ga_email_address'].'|password:'.$_POST['ga_password'];
		$db->db_query("UPDATE ".TABLE_ADMIN_START_PAGE." SET parameter = '".$params."' WHERE php_name = 'google_analytics' ");
		$msg = 'Die Anmeldedaten wurden in der Datenbank abgelegt.';
	}
	$ga = new gapi($_POST['ga_email_address'], $_POST['ga_password']);
	$_SESSION['ga_auth_token'] = $ga->getAuthToken();

} elseif(isset($_POST['submit_data']) && (!isset($_POST['ga_email_address'])) && (!isset($_POST['ga_password'])))
	$msg = 'Geben Sie eine Email-Adresse und ein Passwort an!';

if(isset($_POST['ga_email_address']) && isset($_POST['ga_password']) && !isset($_POST['set_ga_id'])) {
	$ga = new gapi($_POST['ga_email_address'], $_POST['ga_password']);
	$ga->requestAccountData();

	$c .= '<table width="100%" class="dataTable">';
	$c .= '<tr><td colspan="2"><b>Welches Konto soll geladen werden?</b></td></tr>';
	foreach($ga->getResults() as $result) {
		$c .= '	<tr>
					<td>'.$result->getTitle().'</td>
					<td>
						<form action="start.php" method="POST">
							<input type="hidden" name="ga_id" value="'.$result->getProfileId().'" />
							<input type="hidden" name="ga_email_address" value="'.$_POST['ga_email_address'].'" />
							<input type="hidden" name="ga_password" value="'.$_POST['ga_password'].'" />
							<input type="checkbox" name="save_ga_id" value="yes" /> merken? <button type="submit" class="button" name="set_ga_id" value="load">Konto abrufen</button>
						</form>
					</td>
				</tr>';
	}
	$c .= '</table>';

} elseif(isset($_POST['set_ga_id']) && ($_POST['set_ga_id'] == 'load')) {

	$data = $db->db_query("SELECT parameter FROM ".TABLE_ADMIN_START_PAGE." WHERE php_name = 'google_analytics'");
	
	$params = 'email:'.$_POST['ga_email_address'].'|password:'.$_POST['ga_password'];
	
	if($data->_numOfRows > 0) {
		if(isset($_POST['save_ga_id']) && ($_POST['save_ga_id'] == 'yes')) {
			$params .= $data->fields['parameter'].'|id:'.$_POST['ga_id'];
			$db->db_query("UPDATE ".TABLE_ADMIN_START_PAGE." SET parameter = '".$params."' WHERE php_name = 'google_analytics' ");
			$data = $db->db_query("SELECT parameter FROM admin_start_page WHERE php_name = 'google_analytics'");
		}

		$ga_data = explode('|', $data->fields['parameter']);
		foreach($ga_data AS $gd) {
			$key = explode(':', $gd);
			$vars[$key[0]] = $key[1];
		}
		if($vars['email']!='' && $vars['password'] !='' && $vars['id'] !='')
			$data_ok = true;
	}
}

if($data_ok) {
	$ga = new gapi($vars['email'], $vars['password']);
	$ga->requestReportData($vars['id'],
							array('referralPath', 'keyword', 'visitCount', 'browser', 'browserVersion', 'pagePath', 'landingPagePath'),
							array('pageviews','visits'));
	$c .= '<script type="text/javascript">
				$(function() {
					$("#ga").tabs({fx:{opacity:"toggle"}});
				});
			</script>';
	$c .= '<div class="ar"><a href="start.php?ga=delete" class="button">Google-Analytics Daten löschen</a></div>';
	$c .= '<div id="ga">';
	$c .= '	<ul>';
	$c .= '		<li><a href="#infos">Infos</a></li>';
	$c .= '		<li><a href="#visitors">Besucher</a></li>';
	$c .= '		<li><a href="#sites">Seiten</a></li>';
	$c .= '		<li><a href="#browser">Browser</a></li>';
	$c .= '	</ul>';

	$c .= '<div id="infos"><table width="100%" class="dataTable">';
	$c .= '	<tr>
				<td>Konto-Name:</td>
				<td>'.$ga->getAccountName().'</td>
			</tr>';
	$c .= '	<tr>
				<td>UA-Nummer:</td>
				<td>'.$ga->getWebPropertyId().'</td>
			</tr>';
	$c .= '	<tr>
				<td>letztes Update:</td>
				<td>'.$ga->getUpdated().'</td>
			</tr>';
	$c .= '	<tr>
				<td>Gesamt-Seitenaufrufe:</td>
				<td>'.$ga->getPageviews().'</td>
			</tr>';
	$c .= '	<tr>
				<td>Gesamt-Besucher:</td>
				<td>'.$ga->getVisits().'</td>
			</tr>';
	$c .= '	<tr>
				<td>Zeitraum:</td>
				<td>'.date_short($ga->getStartDate().' 00:00:00', true).' - '.date_short($ga->getEndDate().' 00:00:00', true).'</td>
			</tr>';
	$c .= '</table></div>';

	$c .= '	<div id="visitors">';
	$c .= '		<table width="100%" class="dataTable">';
	$c .= '			<tr>
						<td><b>Referer-Pfad</b></td>
						<td><b>Keyword</b></td>
					</tr>';
	foreach($ga->getResults() as $result) {
	$c .= '			<tr>
						<td>'.$result->getReferralPath().'</td>
						<td>'.$result->getKeyword().'</td>
					</tr>';
	}
	$c .= '		</table>';
	$c .= '	</div>';

	$c .= '	<div id="sites">';
	$c .= '		<table width="100%" class="dataTable">';
	$c .= '			<tr>
						<td><b>Eintritts-Pfad</b></td>
						<td><b>Landing Page</b></td>
					</tr>';
	foreach($ga->getResults() as $result) {
	$c .= '			<tr>
						<td>'.$result->getPagePath().'</td>
						<td>'.$result->getLandingPagePath().'</td>
					</tr>';
	}
	$c .= '		</table>';
	$c .= '	</div>';

	$c .= '	<div id="browser">
				<table width="100%" class="dataTable">';
	$c .= '		<tr>
					<td><b>Browser</b></td>
					<td><b>Version</b></td>
					<td><b>Seitenaufrufe</b></td>
				</tr>';
	foreach($ga->getResults() as $results) {
		$c .= '	<tr>
					<td>'.$results->getBrowser().'</td>
					<td>'.$results->getBrowserVersion().'</td>
					<td align="center">'.$results->getPageviews().'</td>
				</tr>';
	}
	$c .= '		</table>
			</div>';
	$c .= '</div>';

} elseif(!isset($_POST['ga_email_address']) && !isset($_POST['ga_password'])) {
	$c .= '<form action="start.php?save=ga" method="POST">
				<table width="100%" class="dataTable">
					<tr>
						<td colspan="2"><b>Anmeldedaten für Google-Analytics</b></td>
					</tr>
					<tr>
						<td>Email-Adresse:</td>
						<td><input type="text" name="ga_email_address" value="'.$_POST['ga_email_address'].'" /></td>
					</tr>
					<tr>
						<td>Passwort:</td>
						<td><input type="password" name="ga_password" value="" /></td>
					</tr>
					<tr>
						<td colspan="2"><input type="checkbox" name="save_permanently" value="yes" /> dauerhaft speichern?</td>
					</tr>
					<tr>
						<td colspan="2" align="right"><button type="submit" class="button" name="submit_data" value="yes">Absenden</button></td>
					</tr>
				</table>
			</form>';
}

if(!empty($msg)) {
	echo '<div class="error_msg">'.$msg.'</div>';
	echo '	<script type="text/javascript">
				setTimeout("$(\'.error_msg\').fadeOut();", 3000);
			</script>';
}
echo $c;